package com.sqlist.share.resolver.impl;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.sqlist.share.MainActivity;
import com.sqlist.share.dto.LocationDTO;
import com.sqlist.share.dto.MessageDTO;
import com.sqlist.share.resolver.DataResolver;
import com.sqlist.share.util.GsonUtils;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;

/**
 * Created by SqList on 2018/6/13.
 */

public class LocationDTODataResolver implements DataResolver {
    private static final String TAG = "Share - LocationDTODataResolver";

    @Override
    public void resolve(String messageString, Channel channel) {
        Log.i(TAG, "resolve " + messageString);
        Type type = new TypeToken<MessageDTO<ConcurrentHashMap<String, LocationDTO>>>(){}.getType();
        MessageDTO<Map<String, LocationDTO>> mapMessageDTO =
                GsonUtils.getInstance().fromJson(messageString, type);

        Map map = mapMessageDTO.getData();
        Bundle bundle = new Bundle();
        bundle.putSerializable("locationMap", (Serializable) map);

        Message message = new Message();
        message.what = LocationDTO.LOCATION;
        message.setData(bundle);
        MainActivity.getHandler().sendMessage(message);
    }
}
